/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import ru.ssau.utils.JMSUtil;


@Entity
@Table(name="Messages")
@NamedQueries({
    @NamedQuery(name=Message.FIND_ALL_MESSAGES, query="SELECT m FROM Message m"),
    @NamedQuery(name=Message.FIND_MESSAGESS_BY_CAPTION, query="SELECT m FROM Message m WHERE UPPER(m.caption) LIKE ?1")})
public class Message implements Serializable {

    @Transient
    public static final String FIND_ALL_MESSAGES = "findAllMessages";
    @Transient
    public static final String FIND_MESSAGESS_BY_CAPTION = "findMessagesByCpation";

    @Id
    @Column(name="MessageID", nullable=false)
    @SequenceGenerator(name="MessageIdGenerator", sequenceName="MessageIDSeq", allocationSize=1)
    @GeneratedValue(generator="MessageIdGenerator")
    private Integer id;

    @Column(name="MsgCaption", nullable=false, length=50)
    private String caption;

    @Column(name="MsgText", nullable=false)
    private String text;

    @Column(name="PostTime", nullable=false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date postDate;

    @ManyToOne(cascade={CascadeType.REFRESH, CascadeType.MERGE})
    @JoinColumn(name="ThreadID", nullable=false)
    private ru.ssau.entities.Thread thread;

    @ManyToOne(cascade={CascadeType.REFRESH, CascadeType.MERGE})
    @JoinColumn(name="UserID", nullable=true)
    private User user;

    public Message() {

    }

    public Message(String caption, String text, Date postDate, User user, Thread thread) {
        this.caption = caption;
        this.text = text;
        this.postDate = postDate;
        this.user = user;
        this.thread = thread;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public Date getPostDate() {
        return postDate;
    }

    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Integer getThreadId() {
        if(thread == null) return null;
        return thread.getId();
    }

    public Integer getUserId() {
        if(user == null) {
            return null;
        } else {
            return user.getId();
        }
    }

    @PostPersist
    private void sendMessageCreatedLogMessage() {
        JMSUtil.sendLogMessage(JMSUtil.MESSAGE, id, new Date(), JMSUtil.ADD);
    }

    @PostRemove
    private void sendMessageRemovedLogMessage() {
        JMSUtil.sendLogMessage(JMSUtil.MESSAGE, id, new Date(), JMSUtil.REMOVE);
    }

    @PostUpdate
    private void sendMessageUpdatedLogMessage() {
        JMSUtil.sendLogMessage(JMSUtil.MESSAGE, id, new Date(), JMSUtil.UPDATE);
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        }
        if(obj == this) {
            return true;
        }
        if(!(obj instanceof Message)) {
            return false;
        }
        Message other = (Message) obj;

        boolean isEqual = true;
        isEqual &= id.equals(other.id);
        isEqual &= caption.equals(other.caption);
        isEqual &= postDate.equals(other.postDate);
        isEqual &= text.equals(other.text);
        isEqual &= thread.equals(other.thread);
        if((user == null) && (other.user == null)) {
            return isEqual;
        } else if((user != null) && (user.equals(other.user))) {
            return isEqual;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 97 * hash + (this.caption != null ? this.caption.hashCode() : 0);
        hash = 97 * hash + (this.text != null ? this.text.hashCode() : 0);
        hash = 97 * hash + (this.postDate != null ? this.postDate.hashCode() : 0);
        hash = 97 * hash + (this.thread != null ? this.thread.hashCode() : 0);
        hash = 97 * hash + (this.user != null ? this.user.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", caption=");
        sb.append(caption);
        sb.append(", text=");
        sb.append(text);
        sb.append(", userId=");
        sb.append((user == null)?"null":user.getId());
        sb.append(", threadId=");
        sb.append(thread.getId());
        sb.append(", postDate=[");
        sb.append(postDate.toString());
        sb.append("]]");
        return sb.toString();
    }

}
